@extends('backend::layout')

@section('title', '学生列表')

@section('content')
    <div class="card border-no">
        <div class="card-body">
            <form method="get">
                <div class="row">
                    <div class="clearfix"></div>
                    <div class="col-md-12 col-sm-12 col-xs-12">
                        <label class="col-md-2 col-sm-12">
                            <input class="form-control" type="text" placeholder="学生姓名" name="child_name" value="{{ old("child_name") ?: (isset($query['child_name']) ? $query['child_name'] : '') }}" />
                        </label>
                        @if($isSchool)
                            <input type="hidden" name="agency_id" value="{{$admin->agency_id}}" />
                            <label class="col-md-2 col-sm-12">
                                <select class="form-control" name="user_id">
                                    <option  value="">--{{__("Teacher")}}--</option>
                                    @foreach($users as $val)
                                        <option value="{{ $val->id }}" @if(old('user_id') === $val->id || (old('user_id') === null && (isset($query['user_id']) && $query['user_id'] ==  $val->id )) ) selected @endif >{{ strlen($val->name) ? $val->name : $val->phone }}</option>
                                    @endforeach
                                </select>
                            </label>
                        @else
                            <label class="col-md-2 col-sm-12">
                                <select class="form-control" id="agency" name="agency_id">
                                    <option  value="">--{{__("Agency")}}--</option>
                                    @foreach($agencies as $val)
                                        <option value="{{ $val->id }}" @if(old('agency_id') === $val->id || (old('agency_id') === null && (isset($query['agency_id']) && $query['agency_id'] ==  $val->id )) ) selected @endif >{{ $val->name }}</option>
                                    @endforeach
                                </select>
                            </label>

                            <label class="col-md-2 col-sm-12">
                                <select class="form-control" id="user" name="user_id">
                                    <option  value="">--{{__("Teacher")}}--</option>
                                    @foreach($users as $val)
                                        <option value="{{ $val->id }}" @if(old('user_id') === $val->id || (old('user_id') === null && (isset($query['user_id']) && $query['user_id'] ==  $val->id )) ) selected @endif >{{ strlen($val->name) ? $val->name : $val->phone }}</option>
                                    @endforeach
                                </select>
                            </label>
                        @endif

                        <label class="col-md-2 col-sm-12">
                            <button class="btn btn-primary btn-sm">搜索</button>
                        </label>
                    </div>
                </div>
            </form>

            <div class="table-responsive">
                <table class="table table-hover">
                    <thead>
                    <tr>
                        <th scope="col">#</th>
                        <th scope="col">{{ __('Username') }}</th>
                        <!--<th scope="col">{{ __('Birthday') }}</th>-->
                    <!--<th scope="col">{{ __('Sex') }}</th>-->
                        <th scope="col">{{ __('Create Time') }}</th>
                        <th scope="col">训练时长</th>
                        <th scope="col">训练总次数</th>
                        <th scope="col">成功次数</th>
                        <th scope="col">失败次数</th>
                        <th scope="col">首次训练</th>
                        <th scope="col">最新一次</th>
                        @if(!$isSchool)
                            <th scope="col">是否有效</th>
                        @endif
                        <th scope="col">{{ __('Options') }}</th>
                    </tr>
                    </thead>
                    <tbody>
                    @if(isset($list))
                        @foreach($list as $idx => $item)
                            <tr>
                                <th scope="row">{{ $idx + 1 }}</th>
                                <td>{{ $item->name }}</td>
                                {{--<td>{{ $item->birthday }}</td>--}}
                                {{--<td>{{ $item->sex == 1 ? "男" : "女" }}</td>--}}
                                <td>{{ $item->created_at }}</td>
                                <td>{{ date("H时i分s秒", $item->durationtotal) }}</td>
                                <td>{{ $item->counttotal }}</td>
                                <td>{{ $item->successnum }}</td>
                                <td>{{ $item->failnum }}</td>
                                <td>{{ $item->firsttime ? $item->firsttime : "-" }}</td>
                                <td>{{ $item->lasttime ? $item->lasttime : "-" }}</td>
                                @if(!$isSchool)
                                    <td>
                                        @if($item->deleted_at)
                                            <span class="label label-warning"> {{ $item->deleted_at ? "已删除" : "" }}</span>
                                        @endif
                                    </td>
                                @endif
                                <td>
                                    @if( $currentAdmin->allow('assessment/index'))
                                        <a href="{{ route('admin.assessment.index', ['child_id' => $item->id]) }}" class="text-error" title="评估测试">&nbsp;&nbsp;<i class="fa fa-table"></i></a>
                                    @endif

                                    @if( $currentAdmin->allow('training/index'))
                                        <a href="{{ route('admin.training.index', ['child_id' => $item->id]) }}" class="text-error" title="训练记录">&nbsp;&nbsp;<i class="fa fa-list"></i></a>
                                    @endif

                                    @if( $currentAdmin->allow('child/view'))
                                        <a href="{{ route('admin.child.view', ['id' => $item->id]) }}" class="text-primary" title="{{ __('View') }}">&nbsp;&nbsp;<i class="fa fa-eye"></i></a>
                                    @endif
                                    @if( $currentAdmin->allow('child/edit'))
                                        <a href="{{ route('admin.child.edit', ['id' => $item->id]) }}" class="text-info" title="{{ __('Edit') }}">&nbsp;&nbsp;<i class="fa fa-pencil"></i></a>
                                    @endif
                                    @if($currentAdmin->allow('child/delete'))
                                        <a href="{{ route('admin.child.delete', ['id' => $item->id]) }}" class="text-danger confirm-delete" title="{{ __('Delete') }}">&nbsp;&nbsp;<i class="fa fa-trash-o"></i></a>
                                    @endif
                                </td>
                            </tr>
                        @endforeach
                    @endif
                    </tbody>
                </table>
            </div>
            <div class="row">
                <div class="clearfix"></div>
                <div class="col-md-12 col-sm-12 col-xs-12 pull-right">
                    @if(isset($list))
                        {{ $list->links() }}
                    @endif
                </div>
            </div>
        </div>
    </div>
@stop


@section('script')
    <script>
        $("document").ready(function (){
            $("#agency").change(function (){
                console.log($(this).val());
                if ($(this).val() > 0){

                    $.getJSON("{{ route("admin.user.ajaxAgencyUser") }}", {agency_id : $(this).val()}, function (data){
                        console.log(data);
                        console.log(data.status);
                        $("#user").empty();
                        if (data.status != 200){

                            console.log(data.status);
                        }else{
                            let str = "<option  value=\"\">--{{__("Teacher")}}--</option>";
                            for (let i in data.users){
                                if (data.users[i].name.length > 0){
                                    str += "<option value=\"" + data.users[i].id + "\">" + data.users[i].name + "</option>";
                                }else {
                                    str += "<option value=\"" + data.users[i].id + "\">" + data.users[i].phone + "</option>";
                                }

                            }
                            $("#user").append(str);

                        }
                    });
                }else{
                    $("#user").html("<option  value=\"\">--{{__("Teacher")}}--</option>");
                    $("#user").find("option").eq(0).prop("selected",true);
                }
            });
        });
    </script>
@stop